package com.moon;

import java.util.ArrayList;

import cn.yueliangbaba.R;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.util.ToastUtil;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ChooseClassActivity extends BaseActivity implements OnClickListener{
	ArrayList<JSONObject> mChildIdentity = new ArrayList<JSONObject>();
	int mChildCount = 0;
	JSONObject mParam = new JSONObject();
	int mUserId = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_choose_class);
		
		findViewById(R.id.left).setOnClickListener(this);
		((TextView) findViewById(R.id.center)).setText("请确认您的身份");
		
		JSONObject mInfo = JSON.parseObject(getIntent().getStringExtra("info"));
		if(mInfo == null)
		{
			return;
		}
		
		JSONObject member = mInfo.getJSONObject("member");
		JSONArray children = member.getJSONArray("children");
		JSONArray classes = member.getJSONArray("classes");
		mUserId = member.getIntValue("id");
		
		JSONArray identify = mInfo.getJSONObject("identitylabel").getJSONArray("identitylabel");
		for(int i = 0; i < identify.size(); i++)
		{
			if(identify.getJSONObject(i).getInteger("type") == 0)
			{
				mChildIdentity.add(identify.getJSONObject(i));
			}
		}
		
		LinearLayout layout = (LinearLayout)findViewById(R.id.layout0);
		LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		View view = mInflater.inflate(R.layout.item_class, null);
		layout.addView(view);
		view.findViewById(R.id.sep).setVisibility(View.VISIBLE);
		((TextView)view.findViewById(R.id.text)).setText("您的姓名");
		((TextView)view.findViewById(R.id.value)).setText(member.getString("username"));
		((TextView)view.findViewById(R.id.value)).setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);

		view = mInflater.inflate(R.layout.item_class, null);
		layout.addView(view);
		((TextView)view.findViewById(R.id.text)).setText("您的电话");
		((TextView)view.findViewById(R.id.text)).setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_phone, 0, 0, 0);
		((TextView)view.findViewById(R.id.value)).setText(member.getString("telephone"));
		((TextView)view.findViewById(R.id.value)).setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
		
		if(classes.size() > 0)
		{
			view = mInflater.inflate(R.layout.item_class, null);
			layout.addView(view);
			view.findViewById(R.id.sep).setVisibility(View.VISIBLE);
			((TextView)view.findViewById(R.id.text)).setText("所在班级");
			((TextView)view.findViewById(R.id.text)).setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
			view.findViewById(R.id.value).setVisibility(View.INVISIBLE);
			
			int size = classes.size();
			for(int i = 0; i < size; i++)
			{
				JSONObject item = classes.getJSONObject(i);
				
				view = mInflater.inflate(R.layout.item_class, null);
				layout.addView(view);
				((TextView)view.findViewById(R.id.text)).setText(item.getString("school_name") + item.getString("name"));
				((TextView)view.findViewById(R.id.text)).setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
				((TextView)view.findViewById(R.id.value)).setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
			}
		}
		
		mChildCount = children.size();
		if(mChildCount > 0)
		{
			view = mInflater.inflate(R.layout.item_class, null);
			layout.addView(view);
			view.findViewById(R.id.sep).setVisibility(View.VISIBLE);
			((TextView)view.findViewById(R.id.text)).setText("您的小孩");
			((TextView)view.findViewById(R.id.text)).setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
			view.findViewById(R.id.value).setVisibility(View.INVISIBLE);
			
			for(int i = 0; i < mChildCount; i++)
			{
				JSONObject item = children.getJSONObject(i);
				
				view = mInflater.inflate(R.layout.item_class, null);
				view.setTag(item.getInteger("student_id"));
				view.setOnClickListener(this);
				layout.addView(view);
				((TextView)view.findViewById(R.id.text)).setText(item.getString("name"));
				((TextView)view.findViewById(R.id.text)).setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
			}
		}
		
		findViewById(R.id.next).setOnClickListener(this);
		findViewById(R.id.kefu).setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		if(arg0.getId() == R.id.next)
		{
			if(mParam.size() < mChildCount)
			{
				ToastUtil.showToastWithAlertPic("请为每一位孩子选择身份");
			}
			else
			{
				Intent intent = new Intent(this,SetPwdActivity.class);
				intent.putExtra("id", mUserId);
				intent.putExtra("param", JSON.toJSONString(mParam));
				startActivity(intent);
			}
			return;
		}
		else if(R.id.left == arg0.getId())
		{
			finish();
			return;
		}
		else if(R.id.kefu == arg0.getId())
		{
			try
			{
				Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:8008786995"));  
				startActivity(intent); 
            }
			catch(Exception e)
			{
				ToastUtil.showToastWithAlertPic("您还没有授权");
			}
			return;
		}
		
		int count = mChildIdentity.size();
		final String[] identify = new String[count];
		for(int i = 0; i < count; i++)
		{
			identify[i] = mChildIdentity.get(i).getString("name");
		}
		
		final View view = arg0;
		new AlertDialog.Builder(this).setTitle("列表框").setItems(identify, new DialogInterface.OnClickListener(){
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				mParam.put(view.getTag().toString(), mChildIdentity.get(arg1).getString("id"));
				((TextView)view.findViewById(R.id.value)).setText(identify[arg1]);
			}
		}).show();
	}
}
